define(function(require,exports,module) {
    var myPub = require('./public');
    var oCutImgShareBtn = $('.js_cutImgShareBtn');

    var oShareImgHtmlShow = $('.js_shareImgBoxShow');

    var oImgBox = $('.imgBox');
    var oSilderImg = $('.js_silderImg');

    var canvasEWMSet = $("#canvasEWMSet");
    var oEwmImgSrc = $(".js_ewmImgSrc");
    var oCloseShareImgBoxBtn = $(".js_closeShareImgBoxBtn");    // 关闭按钮

    var oGoodsName = $('.js_goodsName');
    var oGoodsPrice = $('.js_goodsPrice');
    var _pprice = oGoodsPrice.attr('pprice');
    var _jifen = oGoodsPrice.attr('jifen');

    var oImgInfo = $('.js_imgInfo');

    var ewmUrl = window.location.href;
    ewmUrl = ewmUrl.split('?')[0];

    oShareImgHtmlShow.hide();

    var share = {
        HtmlToImg: function () {
            html2canvas( document.querySelector("#js_shareImgPosition"), {
                // canvas: canvasImgSet,
                useCORS: true,
                // width: window.screen.availWidth,
                // height: window.screen.availHeight,
                // windowWidth: document.body.scrollWidth,
                // windowHeight: document.body.scrollHeight,
                // x:0,
                // y: window.pageYOffset
            }).then(function(canvas) {
                // console.log('Drew on the existing canvas');
                var imgUri = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
                oShareImgHtmlShow.show().find('img').attr('src', imgUri)
            });
        },
        
        SetEWMImg: function (url, fn) {
            canvasEWMSet.qrcode({
                text:url,
                render:'canvas',
                width:'200',
                height:'200',
                minVersion:4,
                radius:0.1,
                fill:'#000',
                background:'#fff',
                // image:$('#img-buffer')[0],
                // mode:4,
                // mSize:0.3,
                // label:'logo'

            });
            if(fn)fn();
        }
        
    }

    var _tapOff = true;
    var time_tapOff = true;

    var oIconList = $('.js_IconList');
    var oIconBtn = $('.js_IconBtn');

    oCutImgShareBtn.on('click', function (e) {
        // console.log(ddjhoc.userAction)
        oIconBtn.removeClass('on');
        oIconList.hide().css({
            'opacity': 0
        });

        if(_tapOff) {
            _tapOff = false;
            // 设置主图
            var _goosName = oGoodsName.text();
            // 设置价格和积分
            var _goodsPrice = '';
            if(_pprice > 0 && _jifen > 0 ) {
                // console.log('_pprice + _jifen')
                _goodsPrice = '￥'+_pprice +'和'+ _jifen + '积分'
            }
            else if(_pprice == 0 && _jifen > 0 ) {
                // console.log('_jifen')
                _goodsPrice = _jifen + '积分'
            }
            else {
                // console.log('_pprice')
                _goodsPrice = '￥'+_pprice
            }

            // ewmUrl = ewmUrl + '?memberId=' + ddjhoc.userAction.memberId + '&openId=' + ddjhoc.userAction.openId;

            oImgInfo.find('h2').text(_goosName);
            oImgInfo.find('p').text(_goodsPrice);

            var _slidImgSrc = oSilderImg.find('.swiper-slide img').eq(0).attr('src');
            oImgBox.find('img').attr('src', _slidImgSrc);

            share.SetEWMImg(ewmUrl, function () {
                // 设置二维码
                var oCanvas = canvasEWMSet.find('canvas')[0];
                var imgUri = oCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
                oEwmImgSrc.attr("src", imgUri) // 将base64格式图片显示到页面上

                // 生产图片
                share.HtmlToImg();
                clearTimeout(time_tapOff);
                time_tapOff = setTimeout(function () {
                    _tapOff = true;
                }, 500);

            });
        }


        // 分享统计
        // if( typeof setActionData != 'undefined' ) {
        //     ddjhoc.setData(oCutImgShareBtn, 'share', 'add', function () {
        //         console.log('ddjhoc.share', ddjhoc.params);
        //         ddjhoc.sendData( ddjhoc.params );
        //     }, setActionData);
        // }

        e.stopPropagation();
    });

    oCloseShareImgBoxBtn.on('click', function (e) {
        oShareImgHtmlShow.hide();
        e.stopPropagation();
    })



});



